package com.doit.day04;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.errors.ProducerFencedException;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

/**
 * 数据从kafka里面读出来，再往kafka里面写
 * 要求写数据的时候开启事务，开启幂等性
 *
 * 精准一次性消费吗？==》终极版本
 */
public class Kafka2Kafka {
    public static void main(String[] args) {
        Properties props = new Properties();
        /**
         * 生产者的
         */
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "linux01:9092");
        props.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        //开启幂等性
        props.setProperty(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true");
        //开启幂等性必须配置的参数
        props.setProperty(ProducerConfig.RETRIES_CONFIG, "3");
        props.setProperty(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, "3");
        props.setProperty(ProducerConfig.ACKS_CONFIG, "-1");
        //开启事务
        props.setProperty(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "C01");
        /**
         * 设置消费者的参数
         */
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "linux01:9092");
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "G001");
        //设置可选的
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);

        //初始化事务
        producer.initTransactions();

        //整一个消费者，来消费kafka中的数据
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        //订阅主题
        consumer.subscribe(Arrays.asList("event-log"));

        while (true) {
            //拉取数据
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            //开启事务
            try {
                for (ConsumerRecord<String, String> record : poll) {
                    producer.beginTransaction();
                    String value = record.value();
                    //数据的封装，发送数据
                    ProducerRecord<String, String> pRecord = new ProducerRecord<>("last", value);
                    producer.send(pRecord);
                    producer.flush();
                    //提交事务   放弃事务也好，都会占用一个offset
                    consumer.commitSync();
                    producer.commitTransaction();
                }
            } catch (ProducerFencedException e) {
                e.printStackTrace();
                //放弃事务
                producer.abortTransaction();
            }
        }


    }
}
